﻿Imports System
Imports System.CodeDom
Imports System.Globalization
Imports System.Reflection

Namespace System.Workflow.ComponentModel.Compiler
    Friend NotInheritable Class DesignTimeFieldInfo
        Inherits FieldInfo
        ' Methods
        Friend Sub New(ByVal declaringType As DesignTimeType, ByVal codeDomField As CodeMemberField)
            If (declaringType Is Nothing) Then
                Throw New ArgumentNullException("Declaring Type")
            End If
            If (codeDomField Is Nothing) Then
                Throw New ArgumentNullException("codeDomEvent")
            End If
            Me.declaringType = declaringType
            Me.codeDomField = codeDomField
            Me.fieldAttributes = Helper.ConvertToFieldAttributes(codeDomField.Attributes)
        End Sub

        Public Overrides Function GetCustomAttributes(ByVal inherit As Boolean) As Object()
            Return Me.GetCustomAttributes(GetType(Object), inherit)
        End Function

        Public Overrides Function GetCustomAttributes(ByVal attributeType As Type, ByVal inherit As Boolean) As Object()
            If (attributeType Is Nothing) Then
                Throw New ArgumentNullException("attributeType")
            End If
            If (Me.attributes Is Nothing) Then
                Me.attributes = Helper.LoadCustomAttributes(Me.codeDomField.CustomAttributes, [TryCast](Me.DeclaringType,DesignTimeType))
            End If
            Return Helper.GetCustomAttributes(attributeType, inherit, Me.attributes, Me)
        End Function

        Public Overrides Function GetValue(ByVal obj As Object) As Object
            Throw New NotImplementedException(TypeSystemSR.GetString("Error_RuntimeNotSupported"))
        End Function

        Public Overrides Function IsDefined(ByVal attributeType As Type, ByVal inherit As Boolean) As Boolean
            If (attributeType Is Nothing) Then
                Throw New ArgumentNullException("attributeType")
            End If
            If (Me.attributes Is Nothing) Then
                Me.attributes = Helper.LoadCustomAttributes(Me.codeDomField.CustomAttributes, [TryCast](Me.DeclaringType,DesignTimeType))
            End If
            Return Helper.IsDefined(attributeType, inherit, Me.attributes, Me)
        End Function

        Public Overrides Sub SetValue(ByVal obj As Object, ByVal value As Object, ByVal invokeAttr As BindingFlags, ByVal binder As Binder, ByVal culture As CultureInfo)
            Throw New NotImplementedException(TypeSystemSR.GetString("Error_RuntimeNotSupported"))
        End Sub


        ' Properties
        Public Overrides ReadOnly Property Attributes As FieldAttributes
            Get
                Return Me.fieldAttributes
            End Get
        End Property

        Public Overrides ReadOnly Property DeclaringType As Type
            Get
                Return Me.declaringType
            End Get
        End Property

        Public Overrides ReadOnly Property FieldHandle As RuntimeFieldHandle
            Get
                Throw New NotImplementedException(TypeSystemSR.GetString("Error_RuntimeNotSupported"))
            End Get
        End Property

        Public Overrides ReadOnly Property FieldType As Type
            Get
                Return Me.declaringType.ResolveType(DesignTimeType.GetTypeNameFromCodeTypeReference(Me.codeDomField.Type, Me.declaringType))
            End Get
        End Property

        Public Overrides ReadOnly Property Name As String
            Get
                Return Helper.EnsureTypeName(Me.codeDomField.Name)
            End Get
        End Property

        Public Overrides ReadOnly Property ReflectedType As Type
            Get
                Return Me.declaringType
            End Get
        End Property


        ' Fields
        Private attributes As Attribute()
        Private codeDomField As CodeMemberField
        Private declaringType As DesignTimeType
        Private fieldAttributes As FieldAttributes
    End Class
End Namespace

